import tiyData from "./collection_data"
import productData from "../../productSelect/js/productData";
import {afterModifyStandsBaseInfo} from "./edit_funs";
import {isNotEmpty} from "../../../js/commonUtils";
import collectionData from "./collection_data";
import {queryBoothItemList} from "./booth_funs";
import {initProductPage, initProductParams} from "../../productSelect/js/productFuns";
import commonData from "./common_data";

export const initCollectionInfo = (self, collectionId) => {
  // resetCollectionBaseInfo();
  tiyData.tData.productAddedList.splice(0);
  let params = {id: collectionId};
  self.$Spin.show();
  self.$apiStand.queryCollectionById(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      let collection = response.result;
      initCollectionBaseInfo(self, collection);
      tiyData.tParam.searchParam.collectionId = tiyData.tData.collectionBaseInfo.id;
      queryAddedProductList(self);
    }else{
      self.$Message.error(response.errorMessage);
    }
  })
}

export const initAddedProductListSearchParam = (collectionId) => {
  tiyData.tParam.searchParam.collectionId = collectionId;
  tiyData.tParam.searchParam.categoryId = "";
  tiyData.tParam.searchParam.categoryLevel = null;
  tiyData.tParam.searchParam.search = "";
  tiyData.tParam.searchParam.state = null;
  tiyData.tParam.searchParam.startTime = null;
  tiyData.tParam.searchParam.endTime = null;
  tiyData.tParam.searchParam.selectedDate = null;
}

export const initCollectionBaseInfo = (self, collection) => {
  tiyData.tData.collectionBaseInfo.id = collection.id;
  tiyData.tData.collectionBaseInfo.type = collection.type;
  tiyData.tData.collectionBaseInfo.state = collection.state;
  tiyData.tData.collectionBaseInfo.title = collection.title;
  tiyData.tData.collectionBaseInfo.imagePc = collection.imagePc;
  tiyData.tData.collectionBaseInfo.imageMobile = collection.imageMobile;
  tiyData.tData.collectionBaseInfo.shortLink = collection.shortLink;
  tiyData.tData.collectionBaseInfo.keyword = collection.keyword;
  tiyData.tData.collectionBaseInfo.hasKeyword = collection.addKeyword==1;
  tiyData.tData.collectionBaseInfo.hasDate = collection.addDateSearch==1;
  tiyData.tData.collectionBaseInfo.hasCategory = isNotEmpty(collection.classificationId);
  tiyData.tData.collectionBaseInfo.publishedStartDate = collection.startDate;
  tiyData.tData.collectionBaseInfo.publishedEndDate = collection.endDate;
  tiyData.tData.collectionBaseInfo.classificationId = collection.classificationId;
  tiyData.tData.collectionBaseInfo.filters.splice(0);
  if(isNotEmpty(collection.filters)) {
    tiyData.tData.collectionBaseInfo.filters.push(...self.$toJson(collection.filters));
  }
  tiyData.tData.collectionBaseInfo.returnFilters.splice(0);
  if(isNotEmpty(collection.returnFilters)) {
    let returnFilters = self.$toJson(collection.returnFilters).hide;
    if(returnFilters != null) {
      tiyData.tData.collectionBaseInfo.returnFilters.push(...returnFilters)
    }
  }
  tiyData.tData.collectionBaseInfo.createtime = collection.createtime;
}

export const initCollectionEditParam = (self, boothItemId) => {
  collectionData.tParam.editParam.collection.boothItemId = boothItemId;
  collectionData.tParam.editParam.collection.id = tiyData.tData.collectionBaseInfo.id;
  collectionData.tParam.editParam.collection.title = tiyData.tData.collectionBaseInfo.title;
  collectionData.tParam.editParam.collection.type = tiyData.tData.collectionBaseInfo.type;
  collectionData.tParam.editParam.collection.keyword = tiyData.tData.collectionBaseInfo.keyword;
  collectionData.tParam.editParam.collection.hasKeyword = tiyData.tData.collectionBaseInfo.hasKeyword;
  collectionData.tParam.editParam.collection.hasDate = tiyData.tData.collectionBaseInfo.hasDate;
  collectionData.tParam.editParam.collection.hasCategory = tiyData.tData.collectionBaseInfo.hasCategory;
  collectionData.tParam.editParam.collection.publishedStartDate = tiyData.tData.collectionBaseInfo.publishedStartDate;
  collectionData.tParam.editParam.collection.publishedEndDate = tiyData.tData.collectionBaseInfo.publishedEndDate;
  collectionData.tParam.editParam.collection.classificationId = tiyData.tData.collectionBaseInfo.classificationId;
  collectionData.tParam.editParam.collection.filters.splice(0);
  collectionData.tParam.editParam.collection.filters.push(...tiyData.tData.collectionBaseInfo.filters);
  collectionData.tParam.editParam.collection.returnFilters.splice(0);
  collectionData.tParam.editParam.collection.returnFilters.push(...tiyData.tData.collectionBaseInfo.returnFilters);
}

export const showAddProductsModal = (self) => {
  //重置产品查询参数
  initProductParams();
  //重置产品列表分页
  initProductPage();
  //重置新增产品参数
  resetProductAddParam();
  // queryProductList(self);
  tiyData.tStatus.product.modalAdd.isShow = true;
}

export const queryProductList = (self) => {
  let params = productData.tParam;
  params.collectionId = tiyData.tData.collectionBaseInfo.id;
  params.pageNum = productData.tPage.pageNum;
  params.pageSize = productData.tPage.pageSize;
  self.$Spin.show();
  self.$apiStand.queryProductList(params).then(response => {
    productData.tData.list.splice(0);
    if(response.success) {
      let page = response.result;
      productData.tData.list.push(...page.list);
      productData.tPage.total = page.total;
    }else{
      productData.tPage.total = 0;
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
}

/**重置产品新增参数*/
export const resetProductAddParam = () => {
  tiyData.tParam.addParam.productSelectedList.splice(0);
  tiyData.tParam.addParam.products.collectionId = tiyData.tData.collectionBaseInfo.id;
  tiyData.tParam.addParam.products.productIds.splice(0);
}

/**重置产品产讯参数*/
export const resetProductSearchParam = () => {
  productData.tParam.search = "";
  productData.tParam.categoryId = "";
  productData.tParam.categoryLevel = null;
  productData.tParam.state = null;
}

export const addProducts = (self) => {
  if(productData.tData.selectedList==null || productData.tData.selectedList.length==0) {
    self.$Message.error("请选择要添加的产品！");
    return;
  }
  tiyData.tParam.addParam.products.productIds = productData.tData.selectedList.map(product => {return product.id});
  let params = tiyData.tParam.addParam.products;
  self.$Spin.show();
  self.$apiStand.addProducts(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("添加产品成功！");
      tiyData.tStatus.product.modalAdd.isShow = false;
      tiyData.tStatus.product.modalSequence.isShow = false;
      afterAddProducts(self);
    }else {
      self.$Message.error(response.errorMessage);
    }
  })
}

export const afterAddProducts = (self) => {
  productData.tParam.collectionId = tiyData.tData.collectionBaseInfo.id;
  tiyData.tParam.searchParam.collectionId = tiyData.tData.collectionBaseInfo.id;
  queryProductList(self);
  queryAddedProductList(self);
  afterModifyStandsBaseInfo(self);
}

/**修改产品序号*/
export const modifyProduct = (self, callback) => {
  self.$refs.modifyProductForm.validate(valid => {
    if(valid) {
      let params = tiyData.tParam.editParam.product;
      self.$Spin.show();
      self.$apiStand.modifyProduct(params).then(response => {
        if(response.success) {
          tiyData.tStatus.product.modalModify.isShow = false;
          self.$Message.success("修改保存成功！");
          afterModifyProduct(self);
        }else{
          self.$Message.error(response.errorMessage);
        }
        self.$Spin.hide();
      });
    }
  });
}

export const afterModifyProduct = (self) => {
  afterModifyStandsBaseInfo(self);
  tiyData.tParam.searchParam.collectionId = tiyData.tData.collectionBaseInfo.id;
  queryAddedProductList(self);
}

export const afterDeleteProduct = (self) => {
  afterModifyStandsBaseInfo(self);
  tiyData.tParam.searchParam.collectionId = tiyData.tData.collectionBaseInfo.id;
  queryAddedProductList(self);
}

export const queryAddedProductList = (self) => {
  let params = tiyData.tParam.searchParam;
  params.pageNum = tiyData.tPage.pageNum;
  params.pageSize = tiyData.tPage.pageSize;
  self.$Spin.show();
  self.$apiStand.queryAddedProductList(params).then(response => {
    tiyData.tData.productAddedList.splice(0);
    if(response.success) {
      let page = response.result;
      tiyData.tData.productAddedList.push(...page.list);
      tiyData.tPage.total = page.total;
    }else{
      self.$Message.error(response.errorMessage);
      tiyData.tPage.total = 0;
    }
    self.$Spin.hide();
  });
}

export const showModifyCollectionModal = (self, boothItemId) => {
  self.$refs.modifyCollection.$refs.collectionForm.resetFields();
  initCollectionEditParam(self, boothItemId);
  tiyData.tStatus.collection.modalModify.isShow = true;
}

export const modifyCollection = (self) => {
  self.$refs.modifyCollection.$refs.collectionForm.validate(valid => {
    if(valid) {
      let params = tiyData.tParam.editParam.collection;
      if(!params.hasDate && !params.hasKeyword && params.classificationId=='' && params.type!=commonData.collectionDic.customType) {
          self.$Modal.warning({title: "警告", content:"请至少填写一种条件！"});
          return;
      }
      //处理日期
      if(params.publishedStartDate && params.publishedStartDate instanceof Object){
        var localDate = params.publishedStartDate;
        params.publishedStartDate = new Date(Date.UTC(localDate.getFullYear(), localDate.getMonth(), localDate.getDate(),0,0,0)).toJSON();
      }
      if(params.publishedEndDate && params.publishedEndDate instanceof Object){
        var localDate = params.publishedEndDate;
        params.publishedEndDate = new Date(Date.UTC(localDate.getFullYear(), localDate.getMonth(), localDate.getDate(),0,0,0)).toJSON();
      }
      self.$Spin.show();
      self.$apiStand.saveOrUpdateCollection(params).then(response => {
        self.$Spin.hide();
        if (response.success) {
          tiyData.tStatus.collection.modalModify.isShow = false;
          initCollectionInfo(self, response.result);
          queryBoothItemList(self);
        }else{
          self.$Message.error(response.errorMessage);
        }
      });
    }else{
      console.log("validate failed")
    }
  });
}

export const getReturnFilterCnNames = (filterNames) => {
  let cnNames = commonData.returnFilters.filter(item => filterNames.includes(item.name)).map((item) => {return item.cnName});
  return cnNames.join(",");
}
